package com.bonc.common.sso;

import com.bonc.common.utils.Result;
import com.bonc.modules.sys.entity.SysUserEntity;
import com.bonc.modules.sys.service.SysUserService;
import com.bonc.modules.sys.service.SysUserTokenService;
import com.dhcc.security.Decrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 *
 * @Title: OA单点登录省大音
 * @Description:
 * @Author:weixiaoguang
 * @Since:2021年10月25日
 * @Version:1.1.0
 */
@RestController
@RequestMapping(value = "/ssoOa")
public class SsoOARestFul {
	private static final Logger LOGGER = LoggerFactory.getLogger(SsoOARestFul.class);
	@Value("${oa.TargetUrl}")
	private String targetUrl;
	@Value("${oa.LoginUrl}")
	private String loginUrl;

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;

	/**
	 *山西移动大音平台在portal单点集成
	 * @param session
	 * @param response
	 * @param request
	 */
	@RequestMapping(value = "/oaLogin",produces = "text/html;charset=utf-8")
	public void sso(HttpSession session, HttpServletResponse response, HttpServletRequest request) {

		String context = request.getContextPath();
		LOGGER.info("context-------------:"+context);
		//String targetUrl = "http://localhost:8001/#/home";
		//String loginUrl = "http://localhost:8001/#/login";
		//String targetUrl = "http://10.211.72.240:9000/quality-enhance#/home";
		//String loginUrl = "http://10.211.72.240:9000/quality-enhance#/login";
		// 从PORTAL登陆，通过userdt参数获取uid
		String usertd = request.getParameter("userdt");
		if (usertd != null && !usertd.equals("")) {
			// 解密PORTAL传入的员工登录账号和员工编号
			LOGGER.info("userdt-------------:"+usertd);
			String userinfo = Decrypt.execute(usertd);
			LOGGER.info("userinfo-------------:"+userinfo);
			// 判断参数值是否合法
			if (userinfo.indexOf("$") != -1) {
				String employeeNumber = "";
				String uid = "";
				try {
					// 获取uid和employeeNumber
					String[] userinfos = userinfo.split("\\$");
					employeeNumber = userinfos[1];
					uid = userinfos[0];
					// 开展自己的业务逻辑
					LOGGER.info("uid--------------:"+uid);
					LOGGER.info("employeeNumber---------------:"+employeeNumber);
					//用户信息
					SysUserEntity user = sysUserService.queryByUserName(uid);
					if(!"".equals(user.getUserId())){
						//生成token，并保存到数据库
						Result r = sysUserTokenService.createToken(user.getUserId());
						String token =  r.get("token").toString();
						LOGGER.info("token--------------:"+token);
						Cookie cookie = new Cookie("token",token);
						cookie.setMaxAge(Integer.MAX_VALUE);
						cookie.setPath("/");
						//cookie.setDomain("http://127.0.0.1:18180");
						response.addCookie(cookie);
						response.sendRedirect(targetUrl);
					}else{
						response.sendRedirect(loginUrl);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				// 向页面弹出验证失败的提示信息，并关闭当前页面
				try {
					response.sendRedirect(loginUrl);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
